package com.dxyxx.framework;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.dxyxx.R;
import com.dxyxx.db.BadgeContentObserver;
import com.dxyxx.db.DataChangeProvider;

/**
 * UI描述：多tab界面，底层多tab,有五个tab，分别是 首页、品类、我的果园、购物车、热评
 * 
 * ActivityGroup太难掌控了，  可以使用Fragment来替代
 * 
 * @author chinesejie@qq.com
 * 
 */
@SuppressWarnings("deprecation")
public class MainActivity extends ActivityGroup {
	public static MainActivity instance = null;
	private static ViewFlipper viewFlipper;
	// 声明LocalActivityManager对象，不用了， tab不支持滑动
	// private GestureDetector m_GestureDetector;
	private LocalActivityManager localActivityManager;

	private static ImageView homepageImage; // 首页
	private static ImageView categoryImage; // 品类
	private static ImageView myCenterImage; // 我的果园
	private static ImageView shoppingCarImage;// 购物车
	private static ImageView commentImage; // 热评

	private static LinearLayout homepageLayout;
	private static LinearLayout categoryLayout;
	private static LinearLayout myCenterLayout;
	private static LinearLayout shoppingCarLayout;
	private static LinearLayout commentLayout;

	private static TextView homepageText; // 首页
	private static TextView categoryText; // 品类
	private static TextView myCenterText; // 我的果园
	private static TextView shoppingCarText;// 购物车
	private static TextView commentText; // 热评

	private static int defaultTextColore;
	private static int themeTextColore;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
		instance = this;
		defaultTextColore = categoryText.getCurrentTextColor();
		themeTextColore = homepageText.getCurrentTextColor();

		// TODO 需要监听购物车数据库的改变、改变购物车的红色数字小图标
		// 构建ViewFlipper对象
		viewFlipper = (ViewFlipper) findViewById(R.id.fliper);
		viewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
		viewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
		viewFlipper.setDisplayedChild(0);
		// 获取Activity消息
		localActivityManager = getLocalActivityManager();
		// 注册一个用于手势识别的类
		// m_GestureDetector = new GestureDetector(this);
		// 添加视图，指定每个视图对应的Activity
		// overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
//		viewFlipper.addView((localActivityManager.startActivity("", new Intent(MainActivity.this, HomepageActivity.class)).getDecorView()), 0);
//		viewFlipper.addView((localActivityManager.startActivity("", new Intent(MainActivity.this, CategoryActivity.class)).getDecorView()), 1);
//		viewFlipper.addView((localActivityManager.startActivity("", new Intent(MainActivity.this, MyCenterActivity.class)).getDecorView()), 2);
//		viewFlipper.addView((localActivityManager.startActivity("", new Intent(MainActivity.this, ShoppingCarActivity.class)).getDecorView()), 3);
//		viewFlipper.addView((localActivityManager.startActivity("", new Intent(MainActivity.this, CommentActivity.class)).getDecorView()), 4);

		// 给ViewFlipper设置一个listener
		// viewFlipper.setOnTouchListener(this);
		// 默认为正在播放页面并设置图标
		// now_playing.setIcon(R.drawable.frame_player_press);
		// 设置相应元素索引显示的子视图
		// 允许长按住ViewFlipper,这样才能识别拖动等手势
		viewFlipper.setLongClickable(true);
		// 监听
		bindListener();
		
	}

	private void initViews() {
	
		// 十大控件
		homepageLayout = (LinearLayout) findViewById(R.id.core_main_homepage_layout);
		categoryLayout = (LinearLayout) findViewById(R.id.core_main_category_layout);
		myCenterLayout = (LinearLayout) findViewById(R.id.core_main_myCenter_layout);
		shoppingCarLayout = (LinearLayout) findViewById(R.id.core_main_shoppingCar_layout);
		commentLayout = (LinearLayout) findViewById(R.id.core_main_comment_layout);
		homepageImage = (ImageView) findViewById(R.id.core_main_homepage);
		categoryImage = (ImageView) findViewById(R.id.core_main_category);
		myCenterImage = (ImageView) findViewById(R.id.core_main_myCenter);
		shoppingCarImage = (ImageView) findViewById(R.id.core_main_shoppingCar);
		commentImage = (ImageView) findViewById(R.id.core_main_comment);

		homepageText = (TextView) findViewById(R.id.homepageText);
		categoryText = (TextView) findViewById(R.id.categoryText);
		myCenterText = (TextView) findViewById(R.id.myCenterText);
		shoppingCarText = (TextView) findViewById(R.id.shoppingCarText);
		commentText = (TextView) findViewById(R.id.commentText);
	}

	/**
	 * 监听本Activity中所要监听的对象
	 */
	public void bindListener() {
		homepageLayout.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				moveToSpecifiedTab(getApplicationContext(), 0);
			}
		});
		categoryLayout.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				moveToSpecifiedTab(getApplicationContext(), 1);
			}
		});
		myCenterLayout.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				moveToSpecifiedTab(getApplicationContext(), 2);
			}
		});
		shoppingCarLayout.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				moveToSpecifiedTab(getApplicationContext(), 3);

			}
		});
		commentLayout.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				moveToSpecifiedTab(getApplicationContext(), 4);
			}
		});
	}

	public static void changeBackground(LinearLayout ll, int num) {
		homepageLayout.setBackgroundResource(R.color.transparent);
		categoryLayout.setBackgroundResource(R.color.transparent);
		myCenterLayout.setBackgroundResource(R.color.transparent);
		shoppingCarLayout.setBackgroundResource(R.color.transparent);
		commentLayout.setBackgroundResource(R.color.transparent);

		homepageImage.setImageResource(R.drawable.homepage_transparent);
		categoryImage.setImageResource(R.drawable.category_transparent);
		myCenterImage.setImageResource(R.drawable.mycenter_transparent);
		shoppingCarImage.setImageResource(R.drawable.shoppingcar_transparent);
		commentImage.setImageResource(R.drawable.comment_transparent);

		homepageText.setTextColor(defaultTextColore);
		categoryText.setTextColor(defaultTextColore);
		myCenterText.setTextColor(defaultTextColore);
		shoppingCarText.setTextColor(defaultTextColore);
		commentText.setTextColor(defaultTextColore);

		switch (num) {
		case 0:
			homepageImage.setImageResource(R.drawable.homepage_transparent_click);
			homepageText.setTextColor(themeTextColore);
			break;
		case 1:
			categoryImage.setImageResource(R.drawable.category_transparent_click);
			categoryText.setTextColor(themeTextColore);
			break;
		case 2:
			myCenterImage.setImageResource(R.drawable.mycenter_transparent_click);
			myCenterText.setTextColor(themeTextColore);
			break;
		case 3:
			shoppingCarImage.setImageResource(R.drawable.shoppingcar_transparent_click);
			shoppingCarText.setTextColor(themeTextColore);
			break;
		case 4:
			commentImage.setImageResource(R.drawable.comment_transparent_click);
			commentText.setTextColor(themeTextColore);
			break;

		default:
			break;
		}
	}

	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// // 一定要将触屏事件交给手势识别类去处理(自己处理会很麻烦的)
	// return false;
	// }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false; // 屏蔽返回键
	}

	// 这里封装几个方法，1）回到主页的方法
	/*
	 * public static void backtoHome(Context context) {
	 * changeBackground(homepageLayout); viewFlipper.setInAnimation(context,
	 * R.anim.push_right_in); viewFlipper.setOutAnimation(context,
	 * R.anim.push_right_out); viewFlipper.setDisplayedChild(0); }
	 */

	public static void moveToSpecifiedTab(Context context, int tabNumber) {

		int childId = viewFlipper.getDisplayedChild();
		if (childId == tabNumber) {
			return;// 啥都不干
		}
		if (childId < tabNumber) {
			viewFlipper.setInAnimation(context, R.anim.push_left_in);
			viewFlipper.setOutAnimation(context, R.anim.push_left_out);
		} else {
			viewFlipper.setInAnimation(context, R.anim.push_right_in);
			viewFlipper.setOutAnimation(context, R.anim.push_right_out);
		}
		switch (tabNumber) {
		case 0:
			changeBackground(homepageLayout, 0);
			viewFlipper.setDisplayedChild(0);
			break;
		case 1:
			changeBackground(categoryLayout, 1);
			viewFlipper.setDisplayedChild(1);
			break;
		case 2:
			changeBackground(myCenterLayout, 2);
			viewFlipper.setDisplayedChild(2);
			break;
		case 3:
			changeBackground(shoppingCarLayout, 3);
			viewFlipper.setDisplayedChild(3);
			break;
		case 4:
			changeBackground(commentLayout, 4);
			viewFlipper.setDisplayedChild(4);
			break;

		default:
			break;
		}
	}
}